package cn.walls1717.wallsmall.coupon.service.impl;

import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.Query;
import cn.walls1717.wallsmall.coupon.dao.SeckillSkuRelationDao;
import cn.walls1717.wallsmall.coupon.entity.SeckillSkuRelationEntity;
import cn.walls1717.wallsmall.coupon.service.SeckillSkuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service("seckillSkuRelationService")
public class SeckillSkuRelationServiceImpl extends ServiceImpl<SeckillSkuRelationDao, SeckillSkuRelationEntity>
    implements SeckillSkuRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<SeckillSkuRelationEntity> lqw = new LambdaQueryWrapper<>();
        String promotionSessionId = (String) params.get("promotionSessionId");
        if (!StringUtils.isEmpty(promotionSessionId)) {
            lqw.eq(SeckillSkuRelationEntity::getPromotionSessionId, promotionSessionId);
        }

        IPage<SeckillSkuRelationEntity> page = this.page(new Query<SeckillSkuRelationEntity>().getPage(params), lqw);

        return new PageUtils(page);
    }

    /**
     * 根据秒杀场次id(promotionSessionId)查询所有秒杀场次与秒杀商品的关联关系
     *
     * @param promotionSessionId promotionSessionId
     * @return 秒杀场次与秒杀商品的关联关系
     */
    @Override
    public List<SeckillSkuRelationEntity> listByPromotionSessionId(Long promotionSessionId) {
        return lambdaQuery().eq(SeckillSkuRelationEntity::getPromotionSessionId, promotionSessionId).list();
    }

}